package com.nbwyctf.helper.result;

import java.util.HashMap;
import java.util.Map;

/**
 * @author chetaifu
 */
public class AjaxResult {
    public static final int SUCCESS = 1;
    public static final int FAIL = 0;
    private Integer errorCode;
    private String message;
    private Object data;

    public Integer getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(Integer errorCode) {
        this.errorCode = errorCode;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public AjaxResult(Integer errorCode, String message, Object data) {
        this.errorCode = errorCode;
        this.message = message;
        this.data = data;
    }

    public static AjaxResult ajaxSuccess() {
        return ajaxSuccess("", null);
    }

    public static AjaxResult ajaxSuccess(String message) {
        return ajaxSuccess(message, null);
    }

    public static AjaxResult ajaxSuccess(Object data) {
        return ajaxSuccess("", data);
    }

    public static AjaxResult ajaxSuccess(String message, Object payload) {
        return new AjaxResult(SUCCESS, message, payload);
    }

    public static AjaxResult ajaxFailed(String errorMessage) {
        return ajaxFailed(errorMessage, null);
    }


    public static AjaxResult ajaxFailed(Object data) {
        return ajaxFailed("", data);
    }

    public static AjaxResult ajaxFailed(String message, Object payload) {
        return new AjaxResult(FAIL, message, payload);
    }

    public Map<String, Object> asMap() {
        Map<String, Object> m = new HashMap<>(3);
        m.put("code", this.errorCode);
        m.put("message", this.message);
        m.put("data", this.data);
        return m;
    }
}
